package edu.gznu.ics.fast.pulsar.service.userService.impl;

import edu.gznu.ics.fast.pulsar.dao.user.UserMapper;
import edu.gznu.ics.fast.pulsar.model.po.user.TdUserPO;
import edu.gznu.ics.fast.pulsar.model.vo.user.TdUserVO;
import edu.gznu.ics.fast.pulsar.service.userService.RoleService;
import edu.gznu.ics.fast.pulsar.service.userService.UserService;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.InvocationTargetException;

import java.util.Collections;
import java.util.List;
import java.util.Set;

/**
 * Created by Y on 2016/7/29.
 */
@Service("userService")
public class UserServiceImply implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RoleService roleService;
    @Autowired
    private PasswordHelper passwordHelper;

    @Override
    public TdUserPO findUserByName(String userName) {
        TdUserPO tdUserPO = userMapper.queryUserInfoByUsername(userName);
        return tdUserPO;
    }

    @Override
    public TdUserPO findUserByName(TdUserVO user) {
        TdUserPO tdUserPO = userMapper.queryUserInfoByUsername(user.getUserName());
        return tdUserPO;
    }

    @Override
    public TdUserPO findUsernameById(Long userId) {
        return userMapper.queryUsernameById(userId);
    }

    @Override
    public List<TdUserPO> findAllUser() {
        return userMapper.queryAllUser();
    }


    /**
     * 根据用户名查找其角色
     *
     * @param userName
     * @return
     */
    public Set<String> findRolesByUsername(String userName) {
        TdUserPO user = findUserByName(new TdUserVO(userName));
        if (user == null) {
            return Collections.EMPTY_SET;
        }
        return roleService.findRoles(user.getRoleIds().toArray(new Long[0]));
    }

    @Override
    public TdUserPO updateUserInfo(TdUserVO user) {


        userMapper.updateUserInfo(user);
        return null;
    }


    @Override
    public void addUser(TdUserVO userVO) {

        passwordHelper.encryptPassword(userVO);

        userMapper.addUser(userVO);
    }

    /**
     * 根据用户名查找其权限
     *
     * @param userName
     * @return
     */
    public Set<String> findPermissionsByUsername(String userName) {
        TdUserPO user = findUserByName(userName);
        if (user == null) {
            return Collections.EMPTY_SET;
        }
        return roleService.findPermissions(user.getRoleIds().toArray(new Long[0]));
    }

    @Override
    /**
     * 修改密码
     * @param userId
     * @param newPassword
     */
    public void changePassword(Long userId, String newPassword) throws InvocationTargetException, IllegalAccessException {
        TdUserPO user = userMapper.queryUsernameById(userId);

        user.setUserPasswd(newPassword);

        passwordHelper.encryptPassword(user);

        TdUserVO uservo = new TdUserVO();
        BeanUtils.copyProperties(uservo, user);
        userMapper.updateUserInfo(uservo);
    }


}
